using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.DALC;
using Workflows.Components.Triggers.Properties;

namespace Workflows.Components.Triggers
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ManualStart:WorkflowTriggerBase
    {


        private ManualTriggerOutput _Output;
        public override ResultBase Output
        {
            get { return this._Output; }
            set { this._Output = (ManualTriggerOutput)value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public ManualStart():base()
        {
            this.ComponentName = "Manual Start";
            this.Description = "Manually start the workflow";
            this.ComponentIcon = Resources.accessibilitycpl_322;
            this.ComponentImage = Resources.tolmcs;
            this.ComponentActivityType = WorkflowActivityType.Trigger;
            this.ComponentCategory = WorkflowComponentCategory.Triggers.ToString();

            this.TriggerType = WorkflowTriggerType.HumanInterference;
            this.TimeSpanUnit = TimeLapseUnit.Second;
            this.Interval = 5;
            this.LastRunTime = DateTime.MinValue;
            this.StartTime = DateTime.Now.AddMinutes(10);
            this.NextRunTime = DateTime.Now.AddDays(1);
            this.IsActive = false;

            this._Output=new ManualTriggerOutput(this.ComponentID,Guid.Empty,Environment.UserName);
        }


        public override ResultBase[] ProcessTrigger(DateTime lastRunTime)
        {
            WorkflowDesign wfDesign =
                WorkflowDesignDALC.GetWorkflowDesign(WorkflowDesignDALC.GetWorkflowIDByComponentID(this.ComponentID));
            if(wfDesign !=null)
            {
                WorkflowTriggerBase trigger = wfDesign.GetTriggerComponent();
                if (trigger != null)
                {
                    trigger.IsActive = false;
                    WorkflowDesignDALC.DisableWorkflowDesign(wfDesign);
                }
            }

            this._Output=new ManualTriggerOutput(this.ComponentID,Guid.Empty,Environment.UserName);
            return new ManualTriggerOutput[] {this._Output};
        }

        

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkflowTriggerType", ((int) this.TriggerType).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TimeSpanUnit", ((int) this.TimeSpanUnit).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Interval", this.Interval.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FirstStartTime", this.StartTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LastRunTime", this.LastRunTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "NextRunTime", this.NextRunTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IsActive", this.IsActive.ToString());
            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                ManualStart manualStart = base.Instantiate(xNode) as ManualStart;

                manualStart.TriggerType =
                    (WorkflowTriggerType) int.Parse(xNode.Attributes.GetNamedItem("WorkflowTriggerType").Value);
                manualStart.TimeSpanUnit = (TimeLapseUnit) int.Parse(xNode.Attributes.GetNamedItem("TimeSpanUnit").Value);
                manualStart.Interval = int.Parse(xNode.Attributes.GetNamedItem("Interval").Value);
                manualStart.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FirstStartTime").Value);
                manualStart.LastRunTime = DateTime.Parse(xNode.Attributes.GetNamedItem("LastRunTime").Value);
                manualStart.NextRunTime = DateTime.Parse(xNode.Attributes.GetNamedItem("NextRunTime").Value);
                manualStart.IsActive = bool.Parse(xNode.Attributes.GetNamedItem("IsActive").Value);

                return manualStart;
            }
            else
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            
        }
    }
}
